// 搜索接口
// ignore_for_file: non_constant_identifier_names

import 'dart:convert';
import 'dart:ffi';

import 'package:http/http.dart' as http;
import 'package:xiecheng_lvyou/model/travel_model.dart';

Map<String, dynamic> Params = {
  "_fxpcqlniredt": "09031014111431397988",
  "__gw_appid": "99999999",
  "__gw_ver": "1.0",
  "__gw_from": "10650013707",
  "__gw_platform": "H5",
  "districtId": -1,
  "groupChannelCode": "tourphoto_global1",
  "type": null,
  "lat": -180,
  "lon": -180,
  "locatedDistrictId": 0,
  "pagePara": {
    "pageIndex": 1,
    "pageSize": 10,
    "sortType": 9,
    "sortDirection": 0
  },
  "imageCutType": 1,
  "head": {},
  "contentType": "json"
};

class TravelDao {
  static Future<TravelItemModel> fetch(
      String groupChannelCode, int pageIndex, int pageSize) async {
    Map<String, dynamic> paramsMap = Params['pagePara'];
    paramsMap['pageIndex'] = pageIndex;
    paramsMap['pageSize'] = pageSize;
    Params['groupChannelCode'] = groupChannelCode;
    // param.set
    // print(Params);
    var url = Uri.https(
      'm.ctrip.com',
      'restapi/soa2/16189/json/searchTripShootListForHomePageV2',
    );

    final response = await http.post(url, body: json.encode(Params));
    if (response.statusCode == 200) {
      Utf8Decoder utf8decoder = const Utf8Decoder(); // fix 中文乱码
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return TravelItemModel.fromJson(result);
    } else {
      throw Exception('Failed to load travel_page.json');
    }
  }
}
